package com.naiterui.ehp.bs.cms.modules.sys.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bs.cms.common.controller.AbstractController;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.sys.entity.Role;
import com.naiterui.ehp.bs.cms.modules.sys.service.IRoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 角色管理
 */
@Validated
@Api(tags = {SwaggerTag.CMS + SwaggerTag.SYS})
@RestController
@RequestMapping("/sys/role")
public class RoleController extends AbstractController {

  @Autowired
  private IRoleService roleService;

  /**
   * 角色列表
   *
   * @return
   */
  @ApiOperation("角色列表")
  @SysLog("角色列表")
  @GetMapping("/list")
  @PreAuthorize("@p.check('sys:role:list')")
  public PageVO<Role> list(PageParamsVO pageParamsVO, Role role) {
    return this.roleService.queryPage(pageParamsVO, role);
  }

  /**
   * 角色列表
   *
   * @return
   */
  @ApiOperation("选择角色列表")
  @SysLog("选择角色列表")
  @GetMapping("/select")
  @PreAuthorize("@p.check('sys:role:select')")
  public Map<Long, List<Role>> select() {
    return this.roleService.getSelect();
  }

  /**
   * 角色信息
   *
   * @return
   */
  @ApiOperation("角色信息")
  @ApiImplicitParam(name = "roleId", value = "角色ID", dataType = "long", paramType = "path", required = true)
  @SysLog("通过ID获取角色信息")
  @GetMapping("/info/{roleId}")
  @PreAuthorize("@p.check('sys:role:info')")
  public Role info(@PathVariable("roleId") Long roleId) {
    return this.roleService.getRoleInfo(roleId);
  }

  /**
   * 保存角色
   */
  @ApiOperation("保存角色")
  @SysLog("保存角色")
  @PostMapping("/save")
  @PreAuthorize("@p.check('sys:role:save')")
  public void save(@Valid @RequestBody Role role) {
    this.roleService.save(role);
  }

  /**
   * 修改角色
   */
  @ApiOperation("修改角色")
  @SysLog("修改角色")
  @PostMapping("/update")
  @PreAuthorize("@p.check('sys:role:update')")
  public void update(@Valid @RequestBody Role role) {
    this.roleService.update(role);
  }

  /**
   * 删除角色
   */
  @ApiOperation("删除角色")
  @ApiImplicitParam(name = "roleIds", value = "角色ID集合", dataType = "array", paramType = "body", required = true)
  @SysLog("删除角色")
  @PostMapping("/delete")
  @PreAuthorize("@p.check('sys:role:delete')")
  public void delete(@RequestBody Long[] roleIds) {
    this.roleService.deleteBatch(roleIds);
  }
}
